<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    	<title>终端</title>
    	<style>
			*::selection {
				background-color: #000;
				color: white;
			}

			*::-moz-selection {
				background-color: #000;
				color: white;
			}

			*::-webkit-selection {
				background-color: #000;
				color: white
			}
			body, textarea  { font-family: 'Lucida Grande', 'Lucida Sans Unicode', 'Geneva',
						   'Verdana', sans-serif; color: #000; text-align: center;
						   margin: 1em; padding: 0; font-size: 15px; }
			textarea        { background-color: #fff; margin: 0; text-align: left;
						   outline: none !important; }
			textarea [type="submit"] { padding: 3px 6px; }
			pre, code	 { font-family: 'Consolas', 'Monaco', 'Bitstream Vera Sans Mono',
						   monospace; font-size: 14px; }
			div.debugger { text-align: left; padding: 12px; margin: auto;
						   background-color: white; }
			div.traceback, div.plain { border: 1px solid #ddd; margin: 0 0 1em 0; padding: 10px; }
			div.plain p      { margin: 0; }
			div.plain textarea,
			div.plain pre { margin: 10px 0 0 0; padding: 4px;
						    background-color: #E8EFF0; border: 1px solid #D3E7E9; }
			div.plain textarea { width: 99%; height: 300px; }
			div.traceback h3 { font-size: 1em; margin: 0 0 0.8em 0; }
			div.traceback ul { list-style: none; margin: 0; padding: 0 0 0 1em; }
			div.traceback h4 { font-size: 13px; font-weight: normal; margin: 0.7em 0 0.1em 0; }
			div.traceback pre { margin: 0; padding: 5px 0 3px 15px;
						        background-color: #E8EFF0; border: 1px solid #D3E7E9; }
			div.traceback .library .current { background: white; color: #555; }
			div.traceback .expanded .current { background: #E8EFF0; color: black; }
			div.traceback pre:hover { background-color: #DDECEE; color: black; cursor: pointer; }
			div.traceback div.source.expanded pre + pre { border-top: none; }

			div.traceback span.ws { display: none; }
			div.traceback pre.before, div.traceback pre.after { display: none; background: white; }
			div.traceback div.source.expanded pre.before,
			div.traceback div.source.expanded pre.after {
				display: block;
			}
			div.traceback div.source.expanded span.ws {
				display: inline;
			}
			div.traceback blockquote { margin: 1em 0 0 0; padding: 0; white-space: pre-line; }
			div.traceback img { float: right; padding: 2px; margin: -3px 2px 0 0; display: none; }
			div.traceback img:hover { background-color: #ddd; cursor: pointer;
						              border-color: #BFDDE0; }
			div.traceback pre:hover img { display: block; }
			div.traceback cite.filename { font-style: normal; color: #3B666B; }
			pre.console { border: 1px solid #ccc; background: white!important;
						  color: black; padding: 5px!important;
						  margin: 3px 0 0 0!important; cursor: default!important;
						  max-height: 400px; overflow: auto; }
			pre.console form { color: #555; }
			pre.console input { background-color: transparent; color: #555;
						        width: 90%; font-family: 'Consolas', 'Deja Vu Sans Mono',
						        'Bitstream Vera Sans Mono', monospace; font-size: 14px;
						         border: none!important; }
			pre.console div.traceback,
			pre.console div.box { margin: 5px 10px; white-space: normal;
						          border: 1px solid #11557C; padding: 10px;
						          font-family: 'Lucida Grande', 'Lucida Sans Unicode', 'Geneva',
						          'Verdana', sans-serif;  }
			pre.console div.box h3,
			pre.console div.traceback h3 { margin: -10px -10px 10px -10px; padding: 5px;
						                   background: #11557C; color: white; }

			pre.console div.traceback pre:hover { cursor: default; background: #E8EFF0; }
			pre.console div.traceback pre.syntaxerror { background: inherit; border: none;
						                                margin: 20px -10px -10px -10px;
						                                padding: 10px; border-top: 1px solid #BFDDE0;
						                                background: #E8EFF0; }
			pre.console div.noframe-traceback pre.syntaxerror { margin-top: -10px; border: none; }

			pre.console div.box pre.repr { padding: 0; margin: 0; background-color: white; border: none; }
			pre.console div.box table { margin-top: 6px; }
			pre.console div.box pre { border: none; }
			pre.console div.box pre.help { background-color: white; }
			pre.console div.box pre.help:hover { cursor: default; }
			pre.console table tr { vertical-align: top; }
			div.console { border: 1px solid #ccc; padding: 4px; background-color: #fafafa; }
			div.traceback pre, div.console pre {
				white-space: pre-wrap;       /* css-3 should we be so lucky... */
				white-space: -moz-pre-wrap;  /* Mozilla, since 1999 */
				white-space: -pre-wrap;      /* Opera 4-6 ?? */
				white-space: -o-pre-wrap;    /* Opera 7 ?? */
				word-wrap: break-word;       /* Internet Explorer 5.5+ */
				_white-space: pre;           /* IE only hack to re-specify in
						                     addition to word-wrap  */
			}
		</style>
		<script src="/static/jquery.min.js">
		</script>
		<script src="/static/vue.js"></script>
		<style>
			div code pre.warn{
				margin-top: 10px;
				background: #fff49c;
			}
			div code pre.warn:hover{
				margin-top: 10px;
				background: #fff8c6;
			}
			div code pre.command{
				margin-top: 10px;
				background: rgb(200, 200, 200);
			}
			div code pre.command:hover{
				margin-top: 10px;
				background: rgb(215, 215, 215);
			}
			div code pre.err{
				background: rgb(255, 145, 145);
			}
			div code pre.err:hover{
				background: rgb(255, 175, 175);
			}
		</style>
  	</head>
	<body style="background-color: #fff">
		<div class="debugger">
			<div  class="traceback" id="traceback">
				<pre id="console" class="console"><form id="console_form"> <textarea id="command_input" type="text" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" style='padding: 10px; border:0;height: 100px;padding: 10px;resize: none; max-width: 95%; width: 95%; min-width: 95%; margin-top: 1px; margin: 0px 0px; font-family: "Fira Mono","Monaco","Menlo","Ubuntu Mono","Consolas","source-code-pro",monospace;'></textarea></form></pre>
				<div v-for="(c, i) in his">
					<div v-if="c.command">
						<code>
							<pre class="command" v-bind:id="'cmd_'+i.toString()"><span v-if="ip != c.ip">[ {{c.ip}} ] : </span> {{c.command}}</pre>
						</code>
					</div>
					<div v-if="c.err">
						<code>
							<pre class="err">{{c.err}}</pre>
						</code>
					</div>
					<div v-if="c.std">
						<code>
							<pre>{{c.std}}</pre>
						</code>
					</div>
				</div>
			</div>
		</div>
		<script>
			var app = new Vue({
				el: '#traceback',
				data: {
					his: [],
					ip: ''
				},
				created: function() {
					this.update();
				},
				methods: {
					update: function() {
						$.ajax({
							url:"/history/",
							method:"GET",
							success: (data) => {
								this.his = data.list.reverse();
								this.ip = data.ip;
							}
						})
						$(".command").click((e) => {
							console.log($(e));
						});
					}
				}
			})
			window.onload = () => {
				if(localStorage.getItem("code")) {
					$("#command_input").val(localStorage.getItem("code"))
				}
				setInterval(()=>{
					app.update()
				}, 300);
			};
			$("#command_input").keyup((e) => {
				localStorage.setItem("code", $("#command_input").val());
			});
			$("#command_input").keydown((e) => {
				if(e.ctrlKey && e.keyCode == 13) {
					e.preventDefault();
					var command = $("#command_input").val();
					if(!command) {
						$("#console").after(`<div><code><pre class="warn">命令为空</pre></code></div>`)
						return false;
					}
					$.ajax({
						url:"/api/",
						method:"POST",
						data: { 
							command : command
						},
						success: (data) => {
							console.log(data);
							app.his.unshift(data);
						}
					})
					return false;
				}
				return true;
			});
		</script>
	</body>
</html>
